package com.ruoyi.common.utils.file;

import org.apache.commons.logging.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;

/**
 * @ClassName IOUtils
 * @Description
 * @Author wanghuan
 * @Date 2022/6/27 14:18
 * @Version 1.0
 **/
public class IOUtils {
    public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close)
            throws IOException {
        try {
            copyBytes(in, out, buffSize);
            if(close) {
                out.close();
                out = null;
                in.close();
                in = null;
            }
        } finally {
            if(close) {
                closeStream(out);
                closeStream(in);
            }
        }
    }

    public static void copyBytes(InputStream in, OutputStream out, int buffSize)
            throws IOException {
        PrintStream ps = out instanceof PrintStream ? (PrintStream)out : null;
        byte buf[] = new byte[buffSize];
        int bytesRead = in.read(buf);
        while (bytesRead >= 0) {
            out.write(buf, 0, bytesRead);
            if ((ps != null) && ps.checkError()) {
                throw new IOException("Unable to write to output stream.");
            }
            bytesRead = in.read(buf);
        }
    }

    public static void closeStream(java.io.Closeable stream) {
        cleanup(null, stream);
    }

    public static void cleanup(Log log, java.io.Closeable... closeables) {
        for (java.io.Closeable c : closeables) {
            if (c != null) {
                try {
                    c.close();
                } catch(IOException e) {
                    if (log != null && log.isDebugEnabled()) {
                        log.debug("Exception in closing " + c, e);
                    }
                }
            }
        }
    }
}
